package com.guang.demo1.service.impl;

import com.guang.demo1.dao.AccountDao;
import com.guang.demo1.domain.Account;
import com.guang.demo1.service.AccountService;
import com.guang.demo1.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private TransactionManager txManager;

    @Override
    public void transfer(String out, String in, Double money) {
        try {
            txManager.startTransaction();
            Account outAccount = accountDao.findByName(out);
            Account inAccount = accountDao.findByName(in);
            outAccount.setMoney(outAccount.getMoney() - money);
            inAccount.setMoney(inAccount.getMoney() + money);
            accountDao.edit(outAccount);
            // 没有出现异常！之前一切都是正常的
            int i = 1/0;
            accountDao.edit(inAccount);
            txManager.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            txManager.rollbackAndClose();
        }
    }
}